﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CuaHangLaptop.BIZ;

namespace QuanLy.GUI
{
    public partial class HoaDon_GUI : Form
    {
        List<HoaDon_BIZ> list_HD = new List<HoaDon_BIZ>();
        List<KhachHang_BIZ> list_KH = new List<KhachHang_BIZ>();
        HoaDon_BIZ current_hd = new HoaDon_BIZ();
        bool updated = true;

        public HoaDon_GUI()
        {
            InitializeComponent();
            GridView_HD.AutoGenerateColumns = false;
            Frm_Load();
        }

        void Frm_Load()
        {
            list_HD = current_hd.GetAll_HoaDon();
            GridView_HD.DataSource = list_HD;
            list_KH = current_hd.KhachHang.GetAll_KH();
            cb_KH.DataSource = list_KH;
            cb_KH.DisplayMember = "TenKH";
            cb_KH.ValueMember = "MaKH";
            Disable_All(true);
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            Reset();
            Disable_All(false);
            updated = false;
        }

        private void btn_ChiTiet_Click(object sender, EventArgs e)
        {
            CTHoaDon_GUI frm =  new CTHoaDon_GUI(current_hd);
            frm.Show();
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            try
            {
                if (!updated)
                {
                    bool tt;
                    if (rd_yes.Checked) tt = true;
                    else tt = false;
                    current_hd = new HoaDon_BIZ(-1, txt_MaHD.Text, cb_KH.SelectedValue.ToString(), dt_ngay.Value, decimal.Parse(txt_total.Text), tt);
                    current_hd.TotalPrice();
                    list_HD.Add(current_hd);
                }
                else
                {
                    Get_Val();
                    current_hd.TotalPrice();
                }
                current_hd.Save();
                GridView_HD.DataSource = null;
                GridView_HD.DataSource = list_HD;
                Disable_All(true);
                updated = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi! "+ex.Message);
            }
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void GridView_HD_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (GridView_HD.Columns[e.ColumnIndex].HeaderText == "Xóa")
            {
                current_hd = (HoaDon_BIZ)GridView_HD.Rows[e.RowIndex].DataBoundItem;
                list_HD.Remove(current_hd);
                current_hd.Delete();
                GridView_HD.DataSource = null;
                GridView_HD.DataSource = list_HD;
            }
            if (GridView_HD.Columns[e.ColumnIndex].HeaderText == "Sửa")
            {
                current_hd = (HoaDon_BIZ)GridView_HD.Rows[e.RowIndex].DataBoundItem;
                Disable_All(false);
            }
        }

        private void GridView_HD_SelectionChanged(object sender, EventArgs e)
        {
            if (GridView_HD.SelectedRows.Count > 0)
            {
                current_hd = (HoaDon_BIZ)GridView_HD.SelectedRows[0].DataBoundItem;
                Set_Val();
                Disable_All(true);
            }
        }

        void Reset()
        {
            int id = current_hd.Get_Lasted_ID();
            txt_MaHD.Text = "HD" + (id + 1);
            txt_total.Text = "0";
            cb_KH.SelectedIndex = 0;
            rb_no.Checked = true;
            dt_ngay.Value = DateTime.Now;
        }

        void Set_Val()
        {
            txt_MaHD.Text = current_hd.MaHoaDon;
            cb_KH.SelectedValue = current_hd.KhachHang.MaKH;
            txt_total.Text = current_hd.TongTien.ToString();
            dt_ngay.Value = current_hd.NgayLap;
            if (current_hd.TinhTrang) rd_yes.Checked = true;
            else rb_no.Checked = false;
            dt_ngay.Value = current_hd.NgayLap;
        }

        void Get_Val()
        {
            current_hd.MaHoaDon = txt_MaHD.Text;
            current_hd.KhachHang = current_hd.KhachHang.Get_KH_ByCode(cb_KH.SelectedValue.ToString());
            current_hd.TongTien = decimal.Parse(txt_total.Text);
            if (rd_yes.Checked) current_hd.TinhTrang = true;
            else current_hd.TinhTrang = false;
            current_hd.NgayLap = dt_ngay.Value;
        }

        void Disable_All(bool _disable)
        {
            txt_MaHD.ReadOnly = 
            txt_total.ReadOnly = _disable;
            cb_KH.Enabled = rb_no.Enabled = rd_yes.Enabled = dt_ngay.Enabled = !_disable;
        }
    }
}
